﻿using Shop.Data.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shop.Data.Business
{
    public class ClienteBusiness
    {
    
    public static IList<Cliente> GetListByFilter(string filter)
        {
            using (ShopDbContext ctx = new ShopDbContext())
            {
                return ctx.Clientes.Where(t => t.nombre.Contains(filter)).ToList();
                
        }
}
         public static Cliente Create(Cliente Cliente)
        {
            using (ShopDbContext ctx = new ShopDbContext())
            {
                Cliente clienteDB = ctx.Clientes.Add(Cliente);
                ctx.SaveChanges();
                return clienteDB;
            }
        }
         public static Cliente Update(Cliente cliente)
        {
            using (ShopDbContext ctx = new ShopDbContext()) {

                ctx.Entry(cliente).State = EntityState.Modified;
                ctx.SaveChanges();
                return cliente;
            
            }
        }
        public static Cliente GetById(Guid id)
         {

             using (ShopDbContext ctx = new ShopDbContext())
             {

                 Cliente p = ctx.Clientes.Find(id);
                 if (p == null) 
                 {
                     throw new Exception("Cliente GetById Not");
                 }
                 return p;
             }
         
         }
        public static void Delete(Guid id)
        {

            using (ShopDbContext ctx = new ShopDbContext())
            {
                Cliente p = ctx.Clientes.Find(id);

                if (p == null)
                {
                    throw new Exception("Cliente no Tiene Data");

                }
                ctx.Clientes.Remove(p);
                ctx.SaveChanges();

            }
        }
    }
}